package services;

import domain.entities.*;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;


public class CriteriaImportation extends Criteria{

    private Importacion importacion;
    private Compromisopais pais;
    private Droga droga;
    private Protocolo protocolo;

    public CriteriaImportation(Importacion importacion){
        this.importacion = importacion;
        this.pais = null;

    }

    public void setPais(Compromisopais pais){
        this.pais = pais;
    }

    public void setDroga(Droga droga){
        this.droga = droga;
    }

    public void setProtocolo(Protocolo protocolo){
        this.protocolo = protocolo;
    }

    @Override
    public boolean isIn(Object element){
        boolean isIn = true;
        Importacion other = (Importacion)element;

        isIn &= isSubstring(importacion.getImportPermit(),other.getImportPermit());
        isIn &= (pais == null || pais.equals(other.getCompromisopais()));

        if (droga != null){
            Iterator<DrogaImportacion> itDi = other.getDrogaImportaciones().iterator();
            List<DrogaImportacion> noMostrar = new ArrayList<DrogaImportacion>();
            boolean existeDroga = false;
             while(itDi.hasNext()){
                DrogaImportacion actual = itDi.next();
                if(droga.equals(actual.getDroga())){
                    existeDroga = true;
                }
                else{
                    noMostrar.add(actual);
                }
            }
            other.getDrogaImportaciones().removeAll(noMostrar);
            isIn &= existeDroga;
        }

        if(protocolo != null){
            Iterator<ProtocoloImportacion> itPi = other.getProtocoloImportaciones().iterator();
            boolean existeProt = false;
            while(itPi.hasNext()){
                if(protocolo.equals(itPi.next().getProtocolo())){
                    existeProt = true;
                    break;
                }
            }
        }


        return isIn;
    }
}
